/* @(#)testrecv.c
 * Time-stamp: <2008-10-07 23:30:43 root>
 * Copyright 2008 Julian Qian
 * Author: root@localhost.localdomain
 * Version: $Id: testrecv.c,v 0.0 2008/10/07 14:20:23 root Exp $
 * Keywords: 
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <utmpx.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <math.h>

#define SERVER_PORT 6801
#define	SA	struct sockaddr
#define PROTO_LENG_TMP 52

unsigned int gDispatcher;
char gBuffer[PROTO_LENG_TMP];

void Defense(){
	/* printf("Defense\n"); */
	unsigned int a = 2;
	gDispatcher &= ~a;
	printf("DD-->%u<--\n", gDispatcher);
	fflush(stdout);	
}

void Heart(){
	/* printf("Heart!\n"); */
	unsigned int a = 1;
	gDispatcher &= ~a;
	printf("HH-->%u<--\n", gDispatcher);
	if(gBuffer[0]){
		printf("gBuffer: %s", gBuffer);
	}
	fflush(stdout);
}

void sig_dispatcher_fake(){
	fprintf(stdout,"%d ", gDispatcher++);
	fflush(stdout);
}

void sig_dispatcher(){
	pthread_t pid, pid_1;
	int ret;

	printf("dispatching ...%u\n", gDispatcher);
	if(gDispatcher & 1){
		printf("dispatch Heart: %u\n",gDispatcher);fflush(stdout);
		ret = pthread_create(&pid, NULL, (void *)Heart, NULL);
		if(ret == 0){
			/* printf("pthread created!\n"); */
		}
	}

	if(gDispatcher & 2){
		printf("dispatch Defense: %u\n", gDispatcher);fflush(stdout);
		ret = pthread_create(&pid_1, NULL, (void *)Defense, NULL);
		if(ret == 0){
			/* printf("pthread 1 created!\n"); */
		}
	}

	if(pid){
		pthread_join(pid, NULL);
		/* printf("pthread %d joined\n",pid); */
		gDispatcher |= 1;
	}
	if(pid_1){
		pthread_join(pid_1, NULL);
		/* printf("pthread %d joined\n",pid_1); */
		gDispatcher |= 2;
	}
}

void sig_engine(){
	int res = 0;
	
	signal(SIGALRM, sig_dispatcher);
		
	struct itimerval tick;
	memset(&tick, 0, sizeof(tick));

	/* Timeout to run function first time  */
	tick.it_value.tv_sec = 1;
	tick.it_value.tv_usec = 0;
	
	/* Interval time to run function   */
	tick.it_interval.tv_sec = 1; /* sec */
	tick.it_interval.tv_usec = 0; /* micro sec */
	/* Set timer, ITIMER_REAL : real-time to decrease timer, */
	                         /* send SIGALRM when timeout   */
	res = setitimer(ITIMER_REAL, &tick, NULL);
	if(res){
		fprintf(stderr, "Set timer failed!!\n");
	}

	while(1){
		pause();
	}
	exit(0);
}

void Server(){
	
	int listen_id, accept_id;
	struct sockaddr_in server_addr, client_addr;

	listen_id = socket(AF_INET, SOCK_DGRAM, 0);

	memset(&server_addr, 0, sizeof(server_addr));
	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	server_addr.sin_port = htons(SERVER_PORT);

	bind(listen_id, (SA *)&server_addr, sizeof(server_addr));

	int len, res;

	printf("Server begin...\n");
	memset(gBuffer, 0, PROTO_LENG_TMP);	
	while(1){
		res = recvfrom(listen_id, gBuffer, PROTO_LENG_TMP, 0, (SA *)&client_addr, &len);
		/* printf("buffer: %s\n", gBuffer); */
	}
}

int main(int argc, char *argv[])
{
	gDispatcher = 3;

	int ret;
	pthread_t pid, pid_1;
	printf("create sig_engine\n");
	ret = pthread_create(&pid, NULL, (void *)sig_engine, NULL);
	if(ret){
		perror("pid");
	}

	printf("create Server\n");	
	ret = pthread_create(&pid_1, NULL, (void *)Server, NULL);
	if(ret){
		perror("pid_1");
	}

	pthread_join(pid, NULL);
	pthread_join(pid_1, NULL);
	printf("END!\n");
    return 0;
}

